﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using WebPrice.Models;

namespace WebPrice.DAL
{
    public class InstrumentRepository :IInstrumentRepository, IDisposable
    {
        private PricingDbContext context;

        public InstrumentRepository(PricingDbContext context)
        {
            this.context = context;
        }

        public IEnumerable<Instrument> GetInstruments()
        {
            return context.Instruments.ToList();
        }

        public Instrument GetInstrumentByID(int id)
        {
            return context.Instruments.Find(id);
        }

        public void InsertInstrument(Instrument instrument)
        {
            context.Instruments.Add(instrument);
        }

        public void DeleteInstrument(int instrumentID)
        {
            Instrument Instrument = context.Instruments.Find(instrumentID);
            context.Instruments.Remove(Instrument);
        }

        public void UpdateInstrument(Instrument instrument)
        {
            context.Entry(instrument).State = EntityState.Modified;
        }

        public void Save()
        {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }

}
